<template>
  <div style="border: 1px solid #ccc">
    <Toolbar
      style="border-bottom: 1px solid #ccc"
      :editor="editorRef"
      :defaultConfig="toolbarConfig"
      :mode="MODE"
    />
    <Editor
      style="height: 500px; overflow-y: hidden"
      v-model="valueHtml"
      :defaultConfig="editorConfig"
      :mode="MODE"
      @onCreated="handleCreated"
    />
    <Annotations />
    {{ valueHtml }}
  </div>
</template>

<script lang="ts" setup>
import type { IDomEditor } from "@wangeditor/editor";
import "@wangeditor/editor/dist/css/style.css"; // 引入 css
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { editorRef } from "./index";
import "./extend";
import Annotations from "./extend/annotations/index.vue";

const MODE = "default";
const valueHtml = defineModel<string>({ required: true });

// 内容 HTML
const toolbarConfig = {};
const editorConfig = { placeholder: "请输入内容..." };

// 组件销毁时，也及时销毁编辑器
onBeforeUnmount(() => {
  const editor = editorRef.value;
  if (editor == null) return;
  editor.destroy();
});

/** 编辑器创建完毕时的回调函数。 */
const handleCreated = (editor: IDomEditor) => {
  editorRef.value = editor; // 记录 editor 实例，重要！

  editorRef.value.getConfig().hoverbarKeys.text = {
    menuKeys: [
      "annotations",
      "headerSelect",
      "insertLink",
      "bulletedList",
      "|",
      "bold",
      "through",
      "color",
      "bgColor",
      "clearStyle",
    ], // 定义你想要的 menu keys
  };
};
</script>
